﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Common;
using NanoERP.Business.BillInfo;
using Microsoft.Practices.Unity;

namespace NanoERP
{
    //[Module("FrmQueryBill")]
    public partial class FrmQueryBill : ModuleForm
    {
        readonly BillinfoBLL _billinfoBLL;
        readonly int _billType;

        [InjectionConstructor]
        public FrmQueryBill()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 单据查询
        /// </summary>
        /// <param name="billType">单据类型</param>
        /// <param name="billName">单据名称</param>
        public FrmQueryBill(int billType = 1, string billName = "")
        {
            InitializeComponent();

            _billType = billType;
            Text = string.Format("{0}查询", billName);

            _billinfoBLL = new BillinfoBLL();

            dtBegin.Value = DateTime.Now.AddDays(-7);

            QueryOrder();
        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            string msgLog = string.Empty;
            ExcelUtil.ExportToExcel(this.dgvList, "单据信息", out msgLog);
            if (msgLog != null)
            {
                MessageBox.Show(msgLog, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }

        private void btnClientid_Click(object sender, EventArgs e)
        {
            CreateFormView.BindClientForm(txtClientid);
        }

        private void btnStorageid_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStorageForm(txtStorageid);
        }

        private void btnBusiuserid_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtBusiuserid);
        }

        private void btnCurphaseuserId_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtCurphaseuserId);
        }

        private void btnCreateuserId_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtCreateuserId);
        }

        private void tsbQuery_Click(object sender, EventArgs e)
        {
            QueryOrder();
        }

        void QueryOrder()
        {
            int state = rbState.Checked ? -1 : (rbState1.Checked ? 2 : 1);
            var list = _billinfoBLL.Query(_billType, txtClientid.Text.Length == 0 ? 0 : Normal.ToLong(txtClientid.Tag),
                                                     txtStorageid.Text.Length == 0 ? 0 : Normal.ToInt(txtStorageid.Tag), txtGoodsName.Text,
                                                     txtBusiuserid.Text.Length == 0 ? 0 : Normal.ToLong(txtBusiuserid.Tag),
                                                     txtCurphaseuserId.Text.Length == 0 ? 0 : Normal.ToLong(txtCurphaseuserId.Tag),
                                                     txtCreateuserId.Text.Length == 0 ? 0 : Normal.ToLong(txtCreateuserId.Tag), txtOrderNo.Text,
                                                     txtNote2.Text, dtBegin.Value, dtEnd.Value, state);

            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = list;
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex == -1) return;

            //var obj = dgvList.CurrentRow.DataBoundItem as Billinfo;
            //WinFormsUI.ActivateOrCreateChildForm(new FrmSellReturn(obj), base.DockPanel);
        }
    }
}
